package com.pet.server.ext.action.phone;

import java.util.Date;

import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.data.VerificationCode;
import com.pet.server.ext.service.SystemService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.CookieHelper;
import com.pet.server.ext.util.DateUtil;
import com.pet.server.ext.util.MD5Util;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
public class Bind extends ActionBase{
	
	public String userName,password,nickName,user_type;
	public String code;
	public VerificationCode vcode = null;
	
	public Bind(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		userName = params.getFieldValue("user_name");
		password = params.getFieldValue("password");
		nickName = params.getFieldValue("nick_name");
		user_type=params.getFieldValue("user_type");
		code = params.getFieldValue("code");
		if(StringUtil.isEmpty(userName,password,nickName)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}

		if("phone".equals(user_type)){
		    SystemService systemService = new SystemService();
		    vcode = systemService.findVerification(userName);
		    if(null==vcode || !StringUtil.equals(vcode.getVerification_code(), code) || StringUtil.equals(BaseAttribute.LOGIC_TRUE, vcode.getIs_verified())){
		    	throw ApiError.Type.LOGIC_ERROR.toException("验证码错误!");
		    }
		    long distance = DateUtil.getMinByTwoDate(vcode.getSend_time(), new Date());
		    if(distance>30){
		    	throw ApiError.Type.LOGIC_ERROR.toException("验证码30分钟有效!");
		    }
		    systemService.updateVerification(vcode.getId());
		}
		if("email".equals(user_type)){
			if(!userName.matches("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$")){
				throw ApiError.Type.INVALID_PARAM.toException("邮箱 ["+userName+"]格式不正确!");
			}
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		UserInfo u = userService.findUserInfoByUserName(userName);
		if(u!=null){
			throw ApiError.Type.USER_NAME_ERROR.toException("用户名 ["+userName+"]已经存在!");
		}
		u = userService.findUserInfoBynickName(nickName);
		if(u!=null){
			throw ApiError.Type.NICK_NAME_ERROR.toException("昵称 ["+nickName+"]已经存在!");
		}
		u=new UserInfo();
		u.create_time=new Date();
		u.nick_name=nickName;
		u.user_name=userName;
		u.password = password; //MD5Util.MD5Encode(password, null);
		u.user_type=user_type;
		u = userService.saveUserInfo(u);
		Result result = new Result();
		JSONObject o = new JSONObject();
		o.accumulate("user_name", u.user_name);
		o.accumulate("user_type", u.user_type);
		o.accumulate("nick_name", u.nick_name);
		o.accumulate("user_id", StringUtil.dealNull(u.user_id));
		String cookie = CookieHelper.createCookie(u);
		JSONObject data = new JSONObject();
        data.put("data", o);
		result.setRet(data, cookie);
	    return result;
	}
	
	
}
